layui.use(['layer', 'form', 'table', 'laydate','laypage','admin', 'ax'], function () {
    //var $ = layui.$;
    var $= layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var laypage=layui.laypage;

    /**
     * 系统管理--角色管理
     */
    var Order = {
        tableId: "orderTable",    //表格id
        condition: {
            orderNo: ""
        }
    };

    /**
     * 初始化表格的列
     */
    Order.initColumn = function () {
        return [[
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'orderNo', sort: true, title: '报事单号',width:200},
            {field: 'shopName', sort: true, title: '商户名称',width:200},
            {field: 'shopAdr', sort: true, title: '商户地址',width:200},
            {field: 'contactPhone', sort: true, title: '手机号',width:200},
            {field: 'operatorName', sort: true, title: '处理人',width:200},
            {field: 'describes', sort: true, title: '报事内容',width:472},
            {field: 'repairType', sort: true, title: '报修类型',width:200},
            {field: 'createTime', sort: true, title: '报事时间',width:200},
            {field: 'typeName', sort: true, title: '状态',width:200},
            {field: 'updateTime', sort: true, title: '最新时间',width:200},
            {align: 'left', toolbar: '#tableBar', title: '操作', width: '14%',fixed: 'right'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Order.search = function () {
        var queryData = {};
        //queryData['orderNo'] = Order.condition.orderNo;
        queryData['orderNo'] = $("#orderNo").val().replace(/\s+/g,"");
        queryData['shopName'] = $("#shopName").val().replace(/\s+/g,"");
        queryData['shopAdr'] = $("#shopAdr").val().replace(/\s+/g,"");
        queryData['createTime'] = $("#createTime").val();
        queryData['type'] = $("#type").val();
        queryData['describes'] = $("#describes").val().replace(/\s+/g,"");
        table.reload(Order.tableId,{
            page:{
                curr:1
            },
            where: queryData
        })
    };

    /**
     * 弹出添加角色
     */
    Order.openAddRole = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '新增工单',
            area: '520px',
            content: Feng.ctxPath + '/ticket/add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Order.tableId);
            }
        });
    };
    /**
     * 点击派单
     *
     * @param data 点击按钮时候的行数据
     */
    Order.onDispatchTicket = function (data) {
        admin.putTempData('formOk', false);
        var typeName = encodeURI(data.typeName);
        top.layui.admin.open({
            type: 2,
            title: '派单',
            area: '520px',
            content: Feng.ctxPath + '/staffTicket/ticket_dispatch?orderId=' + data.id+'&typeName='+typeName,
            end: function () {
                admin.getTempData('formOk') && table.reload(Order.tableId);
            }
        });
    };
    /**
     * 点击详情
     *
     * @param data 点击按钮时候的行数据
     */
    Order.onDetailTicket = function (data) {
        admin.putTempData('formOk', false);
        var typeName = encodeURI(data.typeName);
        top.layui.admin.open({
            type: 2,
            area: '1000px',
            title: '工单详情 &nbsp(单号:'+data.orderNo+')',
            content: Feng.ctxPath + '/ticket/ticket_detail?orderId=' + data.id+'&typeName='+typeName,
            end: function () {
                admin.getTempData('formOk') && table.reload(Order.tableId);
            },
            btn: ['返回'],
            btnAlign: 'c'
        });
    };

    /**
     * 点击审核
     *
     * @param data 点击按钮时候的行数据
     */
    Order.onTicketAudit = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            area: '1000px',
            title: '工单审核 ',
            content: encodeURI(Feng.ctxPath + '/ticket/ticket_audit?orderId=' + data.id),
            end: function () {
                admin.getTempData('formOk') && table.reload(Order.tableId);
            }
        });
    };
    /**
     * 点击作废
     *
     * @param data 点击按钮时候的行数据
     */
    Order.onTicketCancel = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            area: '1000px',
            title: '工单作废 ',
            content: encodeURI(Feng.ctxPath + '/ticket/cancel?orderId=' + data.id),
            end: function () {
                admin.getTempData('formOk') && table.reload(Order.tableId);
            }
        });
    };



    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Order.tableId,
        url: Feng.ctxPath + '/ticket/list',
        page: {
            limits:[10,20,30,50,100,'ALL']
        },
        height: "full-158",
        title:"商户工单",
        cellMinWidth: 100,
        cols: Order.initColumn(),
        done:function(res, curr, count){
            if (this.limit ==count){
                $(".layui-laypage-limits select").find("option[value='ALL']").attr("selected",true);
            }
            $(".layui-laypage-limits select").find("option[value='ALL']").attr("value",count).text('全部');
        }
    });
    /**
     * 导出excel按钮
     */
    Order.exportExcel = function () {
        var checkRows = table.checkStatus(Order.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows, 'xls');
        }
    };
    //点击行数据
    $(document).on("click",".layui-table tr td",function() {
        var v = $(this).text();

        var id ="";
        var typeName = "";
        var orderNo = "";
        $(this).parent().children('td').each(function(j){  // 遍历 tr 的各个 td
            if($(this).parent().children('td').length!=1){
                var val = $(this).children('div').children('a').val();
                if(j==0){
                    id=$(this).text()
                }
                if(j==1){
                    orderNo=$(this).text()
                }
                if(j==9){
                    typeName=$(this).text()
                }
            }

        });
        if(id!=""){
            admin.putTempData('formOk', false);
            top.layui.admin.open({
                type: 2,
                area: '1000px',
                title: '工单详情 &nbsp(单号:'+orderNo+')',
                content: Feng.ctxPath + '/ticket/ticket_detail?orderId=' + id+'&typeName='+typeName,
                end: function () {
                    admin.getTempData('formOk') && table.reload(Order.tableId);
                },
                btn: ['返回'],
                btnAlign: 'c'
            });
        }
    });
//渲染时间选择框
    laydate.render({
        elem: '#createTime',
        range: true,
        max: Feng.currentDate()
    });
    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Order.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Order.openAddRole();
    });
    // 导出excel
    $('#btnExp').click(function () {
        Order.exportExcel();
    });
    //重置
    $("#reset").click(function () {
        var queryData = {};
        $("#orderNo").val("");
        $("#shopName").val("");
        $("#shopAdr").val("");
        $("#createTime").val("");
        $("#type").val("");
        $("#describes").val("");
        form.render();
        queryData['orderNo'] = $("#orderNo").val();
        queryData['shopName'] = $("#shopName").val();
        queryData['shopAdr'] = $("#shopAdr").val();
        queryData['createTime'] = $("#createTime").val();
        queryData['type'] = $("#type").val();
        queryData['describes'] = $("#describes").val();
        table.reload(Order.tableId,{
            where: queryData
        })
        table.reload(Order.tableId);


    })

    // 工具条点击事件
    table.on('tool(' + Order.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'dispatch') {
            Order.onDispatchTicket(data);
        } else if (layEvent === 'delete') {
            Order.onDeleteTicket(data);
        } else if (layEvent === 'detail') {
            Order.onDetailTicket(data);
        }else if(layEvent === 'audit'){
            Order.onTicketAudit(data);
        }else if(layEvent === 'cancel'){
            Order.onTicketCancel(data);
        }
        event.stopPropagation();
    });
    //导出全部
    $('#exportAll').on('click', function(){
        // 模拟从后端接口读取需要导出的数据
        var $ = layui.jquery;
        var excel = layui.excel;
        var orderNo = $("#orderNo").val().replace(/\s+/g,"");
        var shopName = $("#shopName").val().replace(/\s+/g,"");
        var shopAdr = $("#shopAdr").val().replace(/\s+/g,"");
        var createTime = $("#createTime").val();
        var type = $("#type").val();
        var describes = $("#describes").val().replace(/\s+/g,"");
        $.ajax({
            url: Feng.ctxPath + '/ticket/export?orderSource=1&orderNo='+orderNo+'&shopName='+shopName+'&shopAdr='+shopAdr+'&createTime='+createTime+'&type='+type+'&describes='+describes,
            dataType: 'json',

            success: function(wa) {
                // layer.close(loading);
                // 假如返回的 res.data 是需要导出的列表数据
                console.log(wa);//dataSource
                // 1. 数组头部新增表头
                // 3. 执行导出函数，系统会弹出弹框
                table.exportFile(tableResult.config.id, wa, 'xls')
            },
            error:function(res){
                layer.msg(res.msg);
            }
        });



    });
});
